package com.xuecheng.media.controller;

import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaAuditVo;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.entity.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
//@RequestMapping("/media")
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;

    public static final Long OPERATION_FLAG = -99887799L;



    @PostMapping("/media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {

        Long companyId = SecurityUtil.getCompanyId();
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(vo);
        mediaDTO.setCompanyId(companyId);
        MediaDTO dto = mediaService.createMedia(mediaDTO);

        return dto;
    }


    @PostMapping("/media/list")
    public PageVO queryCourseAudit(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVo = mediaService.queryMediaBaseList(params,model,companyId);

        return pageVo;
    }

    @DeleteMapping("/media/{maediaId}")
    public void removeMediaId(@PathVariable Long maediaId) {

    }

    @PostMapping("/m/media/list")
    public PageVO queryMediaAudit(PageRequestParams params,@RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVo = mediaService.queryMediaBaseList(params,model,OPERATION_FLAG);

        return pageVo;
    }


    @PutMapping("/m/media/audit")
    public void commitMedia(@RequestBody MediaAuditVo mediaAuditVo) {

        //将 vo 转为 dto 数据
        MediaDTO dto = MediaConvert.INSTANCE.vo22dto(mediaAuditVo);

        //调用service方法进行课程审核
        mediaService.commitMedia(dto);


    }

    @GetMapping("/m/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.previewMedia(mediaId,OPERATION_FLAG);

        return vodUrl;
    }

    @GetMapping("/l/media/{mediaId}")
    public RestResponse getMediaById(@PathVariable Long mediaId) {

        RestResponse<MediaDTO> response = mediaService.getMedia4Service(mediaId);

        return response;
    }

    @GetMapping("/media/preview/{mediaId}")
    public String preview(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.previewMedia(mediaId,companyId);

        return vodUrl;
    }


}
